﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NRCP.BusinessLogicLayer;
using NRCP.Helpers;
using NRCP.BusinessEntities;

namespace NRCP
{
    public partial class Monthly_Plan_Details : System.Web.UI.Page
    {
        private List<District> selectedDistricts = null;

        private string _FishName
        {
            get
            {
                return ViewState["_FishName"].ToString();
            }
            set
            {
                ViewState["_FishName"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PreLoadData();
            }
        }

        private void PreLoadData()
        {
            NationalPlanFishBLL fishBll = new NationalPlanFishBLL();
            NationalPlanFish fish=fishBll.GetNationalPlanFishById(Convert.ToInt32(SiteUtility.GetQueryParameter("fishId")));
            if (fish != null)
            {
                respMessage.SetNoticeMessage("Monthly Sampling Distrubution for " + fish.Fish.Name);
                _FishName = fish.Fish.Name;
            }
            NationalPlanDivisionBLL divisionBll = new NationalPlanDivisionBLL();
            ddlDivision.DataSource = divisionBll.GetNationalPlanDivisionsId(SiteUtility.IntQueryParam);
            ddlDivision.DataTextField = "Value";
            ddlDivision.DataValueField = "Key";
            ddlDivision.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            respMessage.SetNoticeMessage("Monthly Sampling Distrubution for " + _FishName + " in " + ddlDivision.SelectedItem.Text);
            DivisionBLL divisionBll = new DivisionBLL();
            Division division=divisionBll.GetDivisionBySelections(obj => obj.Name.Equals(ddlDivision.SelectedItem.Text));
            if (division != null)
            {
                DistrictBLL districtBll=new DistrictBLL();
                selectedDistricts=districtBll.GetDistricts(obj => obj.DivisionId == division.Id);
            }

            NationalPlanDetailsYearlyBLL nationalPlanDetailsYearlyBLL = new NationalPlanDetailsYearlyBLL();
            List<NationalPlanDetailsYearly> nationalPlanDetailsYearlys = nationalPlanDetailsYearlyBLL.GetNationalPlanDetailsYearlys(obj => obj.NationalPlanId == SiteUtility.IntQueryParam && obj.FishId == Convert.ToInt32(SiteUtility.GetQueryParameter("fishId")) && obj.DivisionId == Convert.ToInt32(ddlDivision.SelectedItem.Value));
            repPlan.DataSource = nationalPlanDetailsYearlys;
            repPlan.DataBind();
        }

        protected void repPlan_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                NationalPlanDetailsYearly yearlyPlan = e.Item.DataItem as NationalPlanDetailsYearly;
                NationalPlanDetailsMonthlyBLL bll = new NationalPlanDetailsMonthlyBLL();
                List<NationalPlanDetailsMonthly> monthlyPlans = bll.GetNationalPlanDetailsMonthliesByNationalPlanDetailsYearlyId(yearlyPlan.Id);
                Repeater repBreakdown = e.Item.FindControl("repBreakdown") as Repeater;
                if (yearlyPlan != null && repBreakdown != null)
                {
                    repBreakdown.DataSource = monthlyPlans;
                    repBreakdown.DataBind();
                }
            }
        }

        public string GetDistrictNameById(Int32 districtId)
        {
            if (selectedDistricts != null)
            {
                return selectedDistricts.FirstOrDefault(obj => obj.Id == districtId).Name;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}